package com.vdong.sharding.rule.database;

import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;

/**
 * 自定义分库逻辑
 */
@Slf4j
public class MyDatabasePreciseShardingAlgorithm implements PreciseShardingAlgorithm<Long> {


    public String doSharding(Collection<String> collection, PreciseShardingValue<Long> preciseShardingValue) {
        Long value = preciseShardingValue.getValue();
        long last4 = value % 10000;
        long dbIndex = last4 % 2;
        String realDb = "ds_" + dbIndex;
        System.out.println("-----------------分库------------------");
        System.out.println("分片值:" + value + ",分配数据库:" + realDb);
        System.out.println("-------------------------------------");
        return realDb;
    }


}
